<?php

namespace App\Dao\System;
use App\Model\System\SystemRolesMenu;
use Yj\Apidog\Annotation\FormData;
use Yj\Daos\BaseDao;
use Hyperf\Database\Model\Builder;
use Yj\Daos\Verify;

/**
 * @var SystemRolesMenu
 */
class SystemRolesMenuDao extends BaseDao
{

    public function DaoWhere(array $params)
    {
        return $this->getDaoQuery($params, function (Verify $verify) {
            $verify('roles_id', function (Builder $query, $roles_id) {
                $query->where('roles_id', $roles_id);
            });
            $verify('menu_id', function (Builder $query, $menu_id) {
                $query->where('menu_id', $menu_id);
            });
        });
    }

    /**
     * 设置菜单
     *
     */
    public function setMenus(int $roles_id, array $menus_id)
    {

        $menus = $this->DaoWhere(compact('roles_id'))->pluck('menu_id');

        collect($menus)->diff($menus_id)->every(function ($menu_id)use($roles_id){
            $this->DaoWhere(compact('roles_id','menu_id'))->delete();
            return true;
        });

        $create_time = $update_time = time();

        $addMenus = collect($menus_id)->diff($menus)->transform(function ($menu_id)use($roles_id,$create_time,$update_time){
            return compact('menu_id','roles_id','create_time','update_time');
        });

        return $this->insert($addMenus->toArray());
    }



}